package com.kevin.design.creationalPattern.simpleFactory;

public class SimpleFactoryOfChart {
    public static void main(String[] args) {
        SimpleFactory simpleFactory1 = SimpleFactory.getInstance();
        SimpleFactory simpleFactory2 = SimpleFactory.getInstance();
        System.out.println(simpleFactory1==simpleFactory2?true:false);
        Chart historgamChart = simpleFactory1.getChart("折线图");
        historgamChart.display();
    }
}

interface Chart{
    public void display();
}

class HistogramChart implements Chart{

    public HistogramChart(){
        System.out.println("创建柱状图");
    }
    @Override
    public void display() {
        System.out.println("显示柱状图");
    }
}

class PieChart implements Chart{
    public PieChart() {
        System.out.println("创建饼状图");
    }

    @Override
    public void display() {
        System.out.println("显示饼状图");
    }
}

class LineChart implements Chart{

    public LineChart() {
        System.out.println("创建折线图");
    }

    @Override
    public void display() {
        System.out.println("显示折线图");
    }
}

class SimpleFactory{
    private SimpleFactory(){

    }
    private static class Instance{
        private final static SimpleFactory factory = new SimpleFactory();
    }
    public static SimpleFactory getInstance(){
        return  Instance.factory;
    }
    public Chart getChart(String chartName){
        Chart chart = null;
        if(chartName.equals("柱状图")){
            chart = new HistogramChart();
        }else if (chartName.equals("饼状图")){
            chart = new PieChart();
        }else if (chartName.equals("折线图")){
            chart = new LineChart();
        }
        return chart;
    }
}


